package com.xiaochi.oss.controller;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.google.common.base.Preconditions;
import com.xiaochi.oss.entity.Result;
import com.xiaochi.oss.service.FileService;
import com.xiaochi.oss.adapter.StorageAdapter;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * 文件存储controller
 * @author zqx
 * @since 2024/8/21
 */
@RestController
@RequestMapping("/file")
public class FileController {


    @Resource
    private FileService fileService;

    @NacosValue(autoRefreshed = true, value = "${storage.service.type}")
    private String storageType;

    @GetMapping("/testGetAllBuckets")
    public String testGetAllBuckets() throws Exception{
        List<String> allBuckets = fileService.getAllBuckets();
        return allBuckets.get(0);
    }

    @GetMapping("/testNacos")
    public String testNacos() throws Exception{
        return storageType;
    }

    /**
     * 上传文件
     */
    @RequestMapping("/upload")
    public Result<String> changeStatus(MultipartFile uploadFile, String bucket, String objectName) {
       String url = fileService.upload(uploadFile,bucket,objectName);
       return Result.ok(url);
    }

}
